﻿using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.DTO;
using Model;
using NPOI.XSSF.UserModel;

namespace API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HouseController : ControllerBase
    {
        public HouseController(HouseNumIBLL Db)
        {
            this.Db = Db;
        }

        public HouseNumIBLL Db { get; }
        [HttpPost]
        public async Task<int> AddHouseNum(HouseNum h)
        {
            return await Db.AddHouseNum(h);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetHouseNums()
        {
            var list = Db.GetHouseNums();
            XSSFWorkbook wk = new XSSFWorkbook();
            var sheet = wk.CreateSheet("房号表");
            var row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("房号id");
            row.CreateCell(1).SetCellValue("房号名称");
            row.CreateCell(2).SetCellValue("状态");
            row.CreateCell(3).SetCellValue("创建时间");
            row.CreateCell(4).SetCellValue("房型编号");
            row.CreateCell(5).SetCellValue("是否入住");
            int s = 1;
            foreach (var i in list)
            { 
                row = sheet.CreateRow(s);
                row.CreateCell(0).SetCellValue(i.HouseNumId);
                row.CreateCell(1).SetCellValue(i.HouseNumName);
                row.CreateCell(2).SetCellValue(i.States?"上架":"下架");
                row.CreateCell(3).SetCellValue(i.CreateTime.ToString("yyyy-MM-dd"));
                row.CreateCell(4).SetCellValue(i.HouseTypeId);
                row.CreateCell(5).SetCellValue(i.HouseState==1?"空闲":"已上架");
                s++;
            }
            MemoryStream ms = new MemoryStream();
            wk.Write(ms);
            byte[] buffer = ms.ToArray();
            return File(buffer, "application/vnd.ms-excel", "房号表.xlsx");
        }
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="h"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Import(IFormFile fl)
        {
            MemoryStream ms = new MemoryStream();
            fl.CopyTo(ms);
            ms.Position = 0;
            XSSFWorkbook wk = new XSSFWorkbook(ms);
            var sheet = wk.GetSheet("房号表");
            int rowCount = sheet.LastRowNum;
            List<HouseNum> house = new List<HouseNum>();
            for (int i = 1; i <= rowCount; i++)
            { 
                HouseNum num = new HouseNum();

                var row = sheet.GetRow(i);
                num.HouseNumName = row.GetCell(1).ToString();
                num.States = Convert.ToBoolean(row.GetCell(2).ToString());
                num.CreateTime = row.GetCell(3).DateCellValue.Value;
                num.HouseTypeId = Convert.ToInt32(row.GetCell(4).ToString());
                num.HouseState = Convert.ToInt32(row.GetCell(5).ToString());
                house.Add(num);
            }

            Db.AddRange(house);
            return Ok();
        }
        [HttpGet]
        public HouseNum Fan(int id)
        {
            return Db.Fan(id);
        }


        [HttpGet]
        public IQueryable<HouseDTO> Show(string? name, DateTime? d1, DateTime? d2)
        {
            return Db.Show(name, d1, d2);
        }
        [HttpPost]
        public Task<int> Update(HouseNum h)
        {
            return Db.Update(h);
        }
        [HttpGet]
        public Task<int> UpdateState(int id)
        {
           return Db.UpdateState(id);
        }
    }
}
